#include "echo.h"
#include <muduo/base/Logging.h>
#include <boost/bind.hpp>

EchoServer::EchoServer(muduo::net::EventLoop *loop, const muduo::net::InetAddress &listenAddr) 
	: server_(loop, listenAddr, "EchoServer") {
	server_.setConnectionCallback(boost::bind(&EchoServer::OnConnection, this, _1));
	server_.setMessageCallback(boost::bind(&EchoServer::OnMessage, this, _1, _2, _3));
}

void
EchoServer::Start() {
	server_.start();
}

void
EchoServer::OnConnection(const muduo::net::TcpConnectionPtr &conn) {
	LOG_INFO << "EchoServer - " << conn->peerAddress().toIpPort() << " -> "
	         << conn->localAddress().toIpPort() << " is "
	         << (conn->connected() ? "UP" : "DOWN");
}

void
EchoServer::OnMessage(const muduo::net::TcpConnectionPtr &conn, muduo::net::Buffer *buf, muduo::Timestamp time) {
	muduo::string msg(buf->retrieveAllAsString());
	LOG_INFO << conn->name() << " echo " << msg.size() << " bytes, "
	         << "data received at " << time.toString();
	conn->send(msg);
}
